<!--
 @description 保修工单-补件列表
 @fileName PbillList.vue
 @author zengqiongying
 @created 2022/05/24 11:13:30
-->
<template>
  <table-box
    ref="table"
    v-loading="isLoading"
    class="custome-table-box"
    no-request
    :data="tableData"
    :table-config="tableConfig"
    show-index
    :table-max-height="200"
  />
</template>

<script>
import { getPBillList } from '@/api/pBill'
export default {
  name: 'RpeairOrderPbillList',
  props: {
    readonly: {
      type: Boolean,
      default: false
    },
    disCode: {
      type: String,
      default: ''
    },
    patchCode: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      isLoading: false,
      tableData: [],
      tableConfig: {
        interfacePath: ``,
        tabIndex: 122,
        hideFuncBar: true,
        setting: [{
          prop: 'auditCode',
          label: '补件单号',
          minWidth: 150
        },
        {
          prop: 'auditState',
          label: '补件状态',
          minWidth: 200,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'after-patch-service',
            dictTypeCode: 'DOCUMENT_STATUS'
          }
        },
        {
          prop: 'disCode',
          label: '经销商编码',
          minWidth: 120
        },
        {
          prop: 'nodeTime',
          label: '节点日期',
          minWidth: 140
        },
        {
          prop: 'logisticsType',
          label: '物流方式',
          minWidth: 200,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'after-patch-service',
            dictTypeCode: 'LOGISTICS_MODE'
          }
        },
        {
          prop: 'factoryName',
          label: '生产工厂',
          minWidth: 200
        },
        {
          prop: 'customerName',
          label: '顾客姓名',
          minWidth: 120
        },
        {
          prop: 'productName',
          label: '产品名称',
          minWidth: 120
        },
        {
          prop: 'productModel',
          label: '产品型号',
          minWidth: 120
        },
        {
          prop: 'plateName',
          label: '补件名称',
          minWidth: 120
        },
        {
          prop: 'quantity',
          label: '数量',
          minWidth: 80
        },
        {
          prop: 'patchReason',
          label: '补件原因',
          minWidth: 200
        },
        {
          prop: 'licensePlateNumber',
          label: '车牌号',
          minWidth: 140
        }]
      }
    }
  },

  computed: {},
  watch: {
    readonly(val, oldVal) {
      this.updateSetting()
    }
  },

  created() {
    this.updateSetting()
    this.getData()
  },
  activated() {},

  mounted() {},

  methods: {
    updateSetting() {
      const index = this.tableConfig.setting.findIndex(v => v.label === '操作')
      if (!this.readonly) {
        if (index === -1) {
          this.tableConfig.setting.push(
            {
              prop: '',
              label: '操作',
              fixed: 'right',
              minWidth: 100,
              render: (_, rows) => {
                return (
                  <el-button size='small' type='text' size='mini' onClick={this.handleDelete.bind(this, rows)}>删除</el-button>
                )
              }
            }
          )
        }
      } else {
        index >= 0 && this.tableConfig.setting.splice(index, 1)
      }
    },
    // 获取补件信息列表
    getData() {
      this.tableData = []
      if (this.patchCode.length === 0) return
      const params = {
        pbCodes: this.patchCode //  'C22052400003,C22052400002'
      }
      this.isLoading = true
      getPBillList(params).then(res => {
        const data = res.data || []
        const codes = this.patchCode.split(',')
        this.tableData = []
        for (const code of codes) {
          const index = data.findIndex(v => v.auditCode === code)
          if (index !== -1) {
            this.tableData.push(data[index])
          }
        }
        this.$emit('data-change', this.tableData)
        this.isLoading = false
      }, () => {
        this.tableData = []
        this.$emit('data-change', this.tableData)
        this.isLoading = false
      })
    },
    // 删行--补件单号
    handleDelete(row) {
      const index = this.tableData.findIndex(v => v.auditCode === row.auditCode)
      this.tableData.splice(index, 1)
      this.$emit('data-change', this.tableData)
    },
    getStatusName(code) {
      const dict = {
        '-2': '作废',
        '-1': '退回',
        '1': '提报',
        '2': '办事处审核通过',
        '3': '客户管理部审核通过',
        '4': '生产管理部审核通过1',
        '5': '生产管理部审核通过2',
        '6': '补件已开销售订单',
        '7': '补件无法生产',
        '9': '补件发货',
        '10': '订单已签收',
        '11': '销售订单生成中'
      }
      return dict[code] || '--'
    }
  }
}

</script>
<style lang='scss' scoped>
  .custome-table-box{
    /deep/ .el-table{
      span{
        text-align: left !important;
      }
    }
  }
</style>
